--
-- Author: yawei.zhu
-- Date: 2018-05-08 10:53:59
--
-- MainTaskPanel
--

local IconRender = require("app.widget.renders.IconRender")

local ClassRef = app.mvc.createPanelClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.MainTaskDesNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
end

function ClassRef:refresh()

    local rootPanel = self._ui["Panel"]
    local rewardPanel = rootPanel:getChildByName("Panel_reward")
    rewardPanel:removeAllChildren()
    local taskIconNode = rootPanel:getChildByName("icon")
    local taskNameNode = rootPanel:getChildByName("name")
    local taskDesNode = rootPanel:getChildByName("Text_mainline")
    local taskTargetNode = rootPanel:getChildByName("Text_tips")
    local taskGradeNode = self._ui["Panel/Panel_bg/bg04"]

    local data = executeFunc(self._onOperate, "getMainTaskData")
    taskNameNode:setString(data.name)
    taskDesNode:setString(data.des)
    taskTargetNode:setString(data.target)

    if data.taskIcon then
        local task = UD:getTaskById(UD:getCurTaskId())
        taskGradeNode:loadTexture(Res.commonUIPath .. "common_cell_rw_1.png")
        task:setTaskIcon(taskIconNode, taskGradeNode)
    end

    -- award
    local function addSingleItem(root, i, dropItem)
        local itemNode = cc.uiloader:load(Res.BaseIcon)
        local rootPanel = itemNode:getChildByName("Panel")
        local x = 0 + (i - 1) * (rootPanel:getContentSize().width + 10)
        local y = 0
        itemNode:setPosition(cc.p(x,y))
        root:addChild(itemNode)

        local uiItem = IconRender.create(itemNode)
        uiItem:setData(dropItem)
        uiItem:setTipEnable(true)
    end

    local dropItems = data.dropItems
    if dropItems and #dropItems > 0 then
        for i,v in ipairs(dropItems) do
            local itemConfig = v
            addSingleItem(rewardPanel,i,v)
        end
    end

    self:setTaskDesc()
end

function ClassRef:setTaskDesc()

    local rootPanel = self._ui["Panel"]
    local digestPanel = rootPanel:getChildByName("Panel_plot")
    local pointRoot = digestPanel:getChildByName("Panel_bom")
    local pointNode = pointRoot:getChildByName("Panel_dot")
    if not self._pointClone then
        self._pointClone = pointNode:clone()
        self._pointClone:retain()
        local pointImage = self._pointClone:getChildByName("dot")
        self._pointColor = pointImage:getColor()
    end
    pointRoot:removeAllChildren()

    local titleName = digestPanel:getChildByName("tlt")
    titleName:setString(L("task_main_desc1"))

    local btnLeft = digestPanel:getChildByName("Button_1")
    local btnRight = digestPanel:getChildByName("Button_2")
    local chapterPanel = digestPanel:getChildByName("Panel_btn")
    local chapterBtn = chapterPanel:getChildByName("Button")
    if not self._chapterBtnClone then
        self._chapterBtnClone = chapterBtn:clone()
        self._chapterBtnClone:retain()
    end
    local cbx = self._chapterBtnClone:getPositionX()
    local cby = self._chapterBtnClone:getPositionY()
    chapterPanel:removeChildByName("Button")

    local pageView = digestPanel:getChildByName("PageView")
    local pageNode = pageView:getChildByName("text")
    local chapterDigests = executeFunc(self._onOperate, "splitTaskDigests")
    if not self._pageNodeClone then
        self._pageNodeClone = pageNode:clone()
        self._pageNodeClone:retain()
    end
    pageView:removeChildByName("text")

    local chapters = #chapterDigests
    if chapters == 0 then return end

    local selPageIndex = 0
    local pages = 0
    local tabBtns = {}
    local pointNodes = {}

    local function setPointBright(pageId)
        for i,v in ipairs(pointNodes) do
            local pointImage = v:getChildByName("dot")
            if pageId == i then
                pointImage:setColor(cc.c3b(255,255,255))
            else
                pointImage:setColor(cc.c3b(self._pointColor.r,self._pointColor.g,self._pointColor.b))
            end
        end
    end

    local function setPageOperation(isShow)
        btnLeft:setVisible(isShow)
        btnRight:setVisible(isShow)
        pointRoot:setVisible(isShow)
    end

    local function setPage(pageId)
        selPageIndex = pageId
        setPointBright(pageId)
        pageView:scrollToPage(pageId - 1)

        if pages <= 1 then
            setPageOperation(false)
        else
            pointRoot:setVisible(true)
            if pageId == 1 then
                btnLeft:setVisible(false)
                btnRight:setVisible(true)
            elseif pageId == pages then
                btnLeft:setVisible(true)
                btnRight:setVisible(false)
            else
                btnLeft:setVisible(true)
                btnRight:setVisible(true)
            end
        end
    end

    btnLeft:addClickEventListener(function(sender)
        local toPage = selPageIndex - 1
        if toPage < 1 then return end
        setPage(toPage)
    end)
    btnRight:addClickEventListener(function(sender)
        local toPage = selPageIndex + 1
        if toPage > pages then return end
        setPage(toPage)
    end)

    local function selectedChapter(chapterId)
        local pageDigest = chapterDigests[chapterId]
        if not pageDigest then return end
        for i,v in ipairs(tabBtns) do
            local nameNode = v:getChildByName("name")
            if i == chapterId then
                v:setBright(false)
                nameNode:setOpacity(255)
            else
                v:setBright(true)
                nameNode:setOpacity(255 / 2)
            end
        end

        pages = #pageDigest

        pageView:removeAllPages()
        pageView:removeAllChildren()
        pageView:forceDoLayout()

        pointRoot:removeAllChildren()
        pointNodes = {}

        if pages <= 1 then
            setPageOperation(false)
        else
            setPageOperation(true)
        end

        local pWidth = self._pointClone:getContentSize().width * pages

        for i,v in ipairs(pageDigest) do
            local pageContainer = self._pageNodeClone:clone()
            pageView:addPage(pageContainer)
            local textNode = pageContainer:getChildByName("Text")
            textNode:setString(v)

            local px = (pointRoot:getContentSize().width - pWidth) / 2 + (i - 1) * self._pointClone:getContentSize().width
            local py = self._pointClone:getContentSize().height / 2
            local pointNode = self._pointClone:clone()
            pointRoot:addChild(pointNode)
            pointNode:setPosition(cc.p(px,py))
            pointNodes[i] = pointNode
        end


        setPage(pages)
    end


    local intervalWidth = chapterPanel:getContentSize().width / chapters

    local intervalX = 80
    for i = 1, chapters do
        local chapterDigest = chapterDigests[i]

        local chapterNode = self._chapterBtnClone:clone()
        chapterPanel:addChild(chapterNode)
        chapterNode:setPosition(cc.p(cbx + intervalX * (i - 1), cby))

        local nameNode = chapterNode:getChildByName("name")
        nameNode:setString(L("chapter_00" .. i)) 

        tabBtns[i] = chapterNode
        chapterNode:addClickEventListener(function(sender)
            selectedChapter(i)
        end)
    end

    selectedChapter(chapters) -- 默认选择最后一个章节
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
